<template>
  <div class="app-container">
    <SearchForm
      v-show="showSearch"
      v-model="queryParams"
      :options="options.formOps"
      @onQuery="getList"
    >
    </SearchForm>
    <el-row :gutter="10" class="mb8">
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <Table
      ref="table"
      :auto-featch="true"
      :fetch-data="fetchData"
      :options="options.table"
      actionWidth="250"
    >
      <template #default>
        <el-table-column label="状态" align="center" width="100">
          <template #default="{ row }">
            <span v-if="row.enableStatus == 0"> 未启用 </span>
            <span v-if="row.enableStatus == 1"> 启用 </span>
            <span v-if="row.enableStatus == 2"> 废止 </span>
          </template>
        </el-table-column>
        <el-table-column
          label="启用时间"
          align="center"
          width="200"
          prop="enableDate"
        >
        </el-table-column>
      </template>
      <template #actionButton="{ row }">
        <el-button
          type="text"
          icon="download"
          :disabled="!row.annexFileUrl"
          @click="handDownload(row)"
          >下载
        </el-button>
        <el-button type="text" icon="Edit" @click="handleEdit(row)"
          >查看
        </el-button>
        <el-button
          v-if="row.enableStatus === 1"
          type="text"
          style="color: red"
          @click="handleQyOrFeizhi(row, 0)"
          >停用</el-button
        >
        <el-button
          type="text"
          v-if="row.enableStatus === 0"
          @click="handleQyOrFeizhi(row, 1)"
          >启用</el-button
        >

        <el-button
          type="text"
          style="color: red"
          v-if="row.enableStatus !== 2"
          @click="handleQyOrFeizhi(row, 2)"
          >废止
        </el-button>
        <el-button
          type="text"
          style="color: red"
          v-if="row.enableStatus === 2"
          @click="handleQyOrFeizhi(row, 1)"
          >取消废止
        </el-button>
      </template>
    </Table>
    <DialogVform
      :formJson="formJson"
      ref="dialogVform"
      @onSubmit="onSubmit"
      :slots="['fileSolt']"
      :showButton="false"
    >
      <template #fileSolt>
        <el-form-item label="文件上传" prop="fileId">
          <FileUpload :limit="1" :fileSize="30" v-model="files"></FileUpload>
        </el-form-item>
      </template>
    </DialogVform>
    <el-dialog append-to-body v-model="qyDialog" width="40%" title="启用">
      <template #default>
        <el-form
          label-width="100px"
          label-position="left"
          ref="refQyForm"
          :model="qiyongForm"
          :rules="rules"
        >
          <el-form-item label="状态">
            <el-select v-model="qiyongForm.enableStatus" size="large">
              <el-option label="未启用" :value="0" />
              <el-option label="启用" :value="1" />
              <el-option label="废止" :value="2" />
            </el-select>
          </el-form-item>
          <el-form-item label="启用日期">
            <el-date-picker
              v-model="qiyongForm.firingDate"
              type="date"
              placeholder=""
              :size="size"
              value-format="YYYY-MM-DD"
              format="YYYY-MM-DD"
            />
          </el-form-item>
          <el-form-item label="启用人">
            <el-input v-model="qiyongForm.enablePerson"></el-input>
          </el-form-item>
        </el-form>
      </template>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="close">取 消</el-button>
          <el-button type="primary" @click="onQySubmit()"> 确 认 </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import Table from "@/components/Table/index.vue";
import SearchForm from "@/components/searchForm/index";
import DialogVform from "@/components/dialogVform/index";
import { apis as APIS } from "@/api/aqsc/flfg copy.js";
import formJson from "./components/options.json";
import { useAsyncData, useFormJson } from "@/views/hooks/useAsyncData.js";
import FileUpload from "@/components/FileUpload";
import { getUserProfile } from "@/api/system/user";

const files = ref([]);

const plugEdit = (row) => {
  if (row.annexFileId) {
    files.value = [
      {
        name: row.annexFileName,
        id: row.annexFileId,
        url: row.annexFileUrl,
      },
    ];
  } else {
    files.value = [];
  }
};
const { proxy } = getCurrentInstance();
const ops = useFormJson(formJson, {
  sort: [],
  sortable: [],
});
const options = reactive({
  table: ops,
  formOps: ops.filter((i) =>
    ["docType", "docNumber", "docName"].includes(i.prop)
  ),
});
const showSearch = ref(true);
const dialogVform = ref();
const table = ref();

const {
  onSubmit,
  fetchData,
  handleAdd,
  handleEdit,
  getList,
  handleDel,
  queryParams,
} = useAsyncData({ APIS, formJson, dialogVform, table, proxy, plugEdit });
// 启用
const qiyongForm = ref({});
const shRow = ref({});
const qyDialog = ref(false);
const refQyForm = ref();
const rules = ref({
  firingDate: [
    {
      required: true,
      message: "字段不能为空",
    },
  ],
  enablePerson: [
    {
      required: true,
      message: "字段不能为空",
    },
  ],
  status: [
    {
      required: true,
      message: "字段不能为空",
    },
  ],
});
const close = () => {
  qyDialog.value = false;
};

const onQySubmit = () => {
  refQyForm.value.validate((valid, fields) => {
    if (valid) {
      APIS.update({
        ...shRow.value,
        ...qiyongForm.value,
      }).then(() => {
        qyDialog.value = false;

        getList();
      });
    } else {
    }
  });
};

const handleQyOrFeizhi = (row, enableStatus) => {
  const plugForm = {};
  if (enableStatus == 1) {
    plugForm.enablePeopleId = userInfo.value.userId;
    plugForm.enablePeople = userInfo.value.nickName;
    plugForm.enableDate = new Date();
  }
  proxy.$modal.confirm("是否确认操作？").then(function () {
    APIS.update({
      ...row,
      enableStatus,
      ...plugForm,
    }).then((res) => {
      getList();
    });
  });
};
const userInfo = ref();
const handDownload = (row) => {
  window.downLoadFile(row.annexFileUrl, row.annexFileName);
};
onMounted(() => {
  getUserProfile().then((res) => {
    userInfo.value = res.data;
    console.log(userInfo.value);
  });
});
</script>

<style lang="scss" scoped></style>
